package com.audamob.audasingers.tool.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.LiL.Wayne.Activity.R;
import com.LiL.Wayne.Activity.activity.Videos_Interface;
import com.audamob.audasingers.constant.DeveloperKey;
import com.audamob.audasingers.model.Song;
import com.audamob.audasingers.tool.view.ImageResizerUtils;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;
import com.google.android.youtube.player.YouTubeThumbnailView.OnInitializedListener;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

public class VideosAdapter extends BaseAdapter {
	private SimpleDateFormat FormatterDuration = new SimpleDateFormat("mm:ss");
	private LayoutInflater mInflater;
	private HashMap<YouTubeThumbnailView, YouTubeThumbnailLoader> loaders;
	Context context;
	ArrayList<Song> ListSong;
	int widthYoutubeMiniature;
	public static int MODE_SIMPLE = 0;
	public static int MODE_CHECK = 1;
	public static int MODE_FAVORITES = 2;
	private int mode;
	private boolean[] hideImageselection;

	class ViewHolderImage {
		YouTubeThumbnailView youtubeVideoMiniature;
		TextView Name;
		TextView youtubeVideoDuration;

		CheckBox checkbox;
		int id;
	}

	public VideosAdapter(Context context, ArrayList<Song> listSongs, int mode,
			boolean[] hideImageImageselection) {
		this.context = context;
		this.ListSong = listSongs;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (mode == MODE_SIMPLE) {
			loaders = new HashMap<YouTubeThumbnailView, YouTubeThumbnailLoader>();
		}
		widthYoutubeMiniature = getWidthYoutubeMiniature();
		this.mode = mode;
		hideImageselection = hideImageImageselection;
	}

	public int getWidthYoutubeMiniature() {

		return (ImageResizerUtils.getDisplayWidthInPx(context) - (int) ImageResizerUtils
				.dpToPx(24, context));// 24 est le margin de View/2
	}

	public int getCount() {
		return ListSong.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolderImage holder;
		if (convertView == null) {
			holder = new ViewHolderImage();

			convertView = mInflater.inflate(R.layout.auda_item_video_custom,
					null);

			Typeface font = Typeface.createFromAsset(context.getAssets(),
					"ExoMedium.otf");

			ImageResizerUtils.setFont(context, (ViewGroup) convertView, font);
			holder.youtubeVideoMiniature = (YouTubeThumbnailView) convertView
					.findViewById(R.id.youtubethumbnailview);

			holder.Name = (TextView) convertView.findViewById(R.id.nam);
			holder.youtubeVideoDuration = (TextView) convertView
					.findViewById(R.id.Number);

			holder.checkbox = (CheckBox) convertView
					.findViewById(R.id.checkbox);
			if (mode == MODE_SIMPLE) {
				holder.youtubeVideoMiniature.setTag(position);
				holder.youtubeVideoMiniature.initialize(
						DeveloperKey.DEVELOPER_KEY, listener);

			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolderImage) convertView.getTag();
			if (mode == MODE_SIMPLE) {
				YouTubeThumbnailLoader loader = null;
				try {
					loader = loaders.get(holder.youtubeVideoMiniature);
				} catch (Exception e) {
					// TODO: handle exception
				}
				if (loader == null) {
					// holder.youtubeVideoMiniature.setTag(position);

				} else {
					// 3) The view is already created and already initialized.
					// Simply set the right videoId
					// on the loader.
					UrlImageViewHelper.setUrlDrawable(
							holder.youtubeVideoMiniature, null,
							R.drawable.bg_opacity);
					try {

						loader.setVideo(ListSong.get(position).getUrl());

					} catch (OutOfMemoryError e) {
						// TODO: handle exception
						Log.d("YoutubeProblem", "error  " + e.getMessage());
					}
				}
			}
		}

		holder.Name.setId(position);
		holder.youtubeVideoDuration.setId(position);

		if (mode == MODE_CHECK) {
			holder.checkbox.setId(position);
			holder.checkbox.setVisibility(View.VISIBLE);
			holder.checkbox.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					CheckBox cb = (CheckBox) v;
					int id = cb.getId();
					if (hideImageselection[id]) {
						cb.setChecked(false);
						hideImageselection[id] = false;
					} else {
						cb.setChecked(true);
						hideImageselection[id] = true;
					}
				}
			});

			holder.checkbox.setChecked(hideImageselection[position]);
		}
		if (mode == MODE_SIMPLE) {
			holder.youtubeVideoMiniature.setId(position);
		}
		/*
		 * Calculate width
		 */
		if (mode == MODE_SIMPLE) {
		holder.youtubeVideoMiniature.getLayoutParams().height = ImageResizerUtils
				.youtubeVideoDimensions(widthYoutubeMiniature);
		holder.youtubeVideoMiniature.requestLayout();
		}
		convertView.findViewById(R.id.ItemSong).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent i = new Intent(context, Videos_Interface.class);
						i.putExtra("name", ListSong.get(position).getName());
						i.putExtra("Url", ListSong.get(position).getUrl());
						i.putExtra("duration", ListSong.get(position)
								.getDuration());
						context.startActivity(i);

					}
				});

		holder.Name.setText(ListSong.get(position).getName());
		holder.youtubeVideoDuration.setText(""
				+ FormatterDuration
						.format(ListSong.get(position).getDuration()));

		holder.id = position;

		return convertView;
	}

	public void releaseLoaders() {
		for (YouTubeThumbnailLoader loader : loaders.values()) {
			loader.release();
		}
	}

	OnInitializedListener listener = new OnInitializedListener() {

		@Override
		public void onInitializationSuccess(YouTubeThumbnailView view,
				YouTubeThumbnailLoader loader) {
			// TODO Auto-generated method stub
			int videoId = (Integer) view.getTag();
			loaders.put(view, loader);

			loader.setVideo(ListSong.get(videoId).getUrl());

		}

		@Override
		public void onInitializationFailure(YouTubeThumbnailView arg0,
				YouTubeInitializationResult arg1) {
			// TODO Auto-generated method stub

		}
	};

}
